package com.xiaohui.com.xiaohui.bean;


import lombok.Data;

import java.util.Collections;
import java.util.List;

@Data
public class PageResponse<T> {
    private Long pageNo = 1L;
    private Long pageSize = 10L;
    private Long total = 0L;
    private Long totalPages = 0L;
    private List<T> records = Collections.emptyList();
    private Long start = 1L;
    private Long end = 0L;

    public PageResponse() {
    }

    public void setPageNo(Long pageNo) {
        if (pageNo == null || pageNo <= 0) {
            this.pageNo = 1L;
        } else {
            this.pageNo = pageNo;
        }
    }

    public void setPageSize(Long pageSize) {
        if (pageSize == null || pageSize <= 0 || pageSize > Integer.MAX_VALUE) {
            this.pageSize = 10L;
        } else {
            this.pageSize = pageSize;
        }
    }

    public void setTotal(Long total) {
        this.total = total;
        if (this.pageSize > 0) {
            this.totalPages = this.total % this.pageSize == 0 ? this.total / this.pageSize : this.total / this.pageSize + 1;
        } else {
            this.totalPages = 0L;
        }
        if (this.pageNo > 0) {
            this.start = (this.pageNo - 1) * this.pageSize + 1;
            this.end = this.start - 1 + this.pageSize;
        } else {
            this.start = 1L;
            this.end = 0L;
        }
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }
}
